package com.xoa.service.equipment.impl;



import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.equipment.LimsCommonCodeMapper;
import com.xoa.dao.equipment.LimsEquipExperienceMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.equipment.LimsCommonCode;
import com.xoa.model.equipment.LimsEquipExperience;
import com.xoa.service.equipment.EquipExperienceService;
import com.xoa.util.DateFormat;
import com.xoa.util.LimsJson;

import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class EquipExperienceServiceImpl implements EquipExperienceService {

    @Autowired
    private LimsEquipExperienceMapper experienceMapper;

    @Autowired
    private LimsCommonCodeMapper limsCommonCodeMapper;
    @Autowired
    private SysCodeMapper sysCodeMapper;
    //@Autowired
    //private LimsCommonCodeMapper limsCommonCodeMapper;
    /**
     * 创建作者:   陈东虎
     * 创建日期:   14:39 2019/11/6
     * 方法介绍:   分页查询所有经验库
     * 参数说明:   * @param
     * @return    LimsJson<LimsEquipExperience>
     */
    @Override
    public  LimsJson<LimsEquipExperience> selectAllEquipmentExperience(HttpServletRequest request, Integer page, Integer limit){
        LimsJson limsJson = new LimsJson(1,"err");
        List<LimsEquipExperience> experiences = new ArrayList<LimsEquipExperience>();
        int count=0;
        try {
            if(page!=null&&limit!=null){
                Map map = new HashMap();
                //封装分页参数
                PageParams pageParams = new PageParams();
                pageParams.setUseFlag(true);
                pageParams.setPage(Integer.valueOf(page));
                pageParams.setPageSize(Integer.valueOf(limit));
                map.put("pageParams", pageParams);
                experiences = experienceMapper.selectAllEquipmentExperience(map);
                for (LimsEquipExperience eq:experiences) {
                    if (eq.getFaultType() != null || !"".equals(eq.getFaultType())) {
                        List<SysCode> sysCodes = sysCodeMapper.selectAllCommonCode(eq.getFaultType());//内部维修方式
                        if (sysCodes.size() > 0) {
                            eq.setFaultType(sysCodes.get(0).getCodeName());
                        }
                    }
                }
                count =  pageParams.getTotal();
            }else{
                experiences = experienceMapper.selectAllExperience();
                count = experienceMapper.countAllExperience();
            }
            if(experiences.size()>0){
                limsJson.setCount(count);
                limsJson.setMsg("查询到"+count+"条数据");
                limsJson.setCode(0);
                limsJson.setData(experiences);
            }else{
                limsJson.setMsg("暂无数据");
                limsJson.setCode(0);
            }
        }catch (Exception e){
            e.printStackTrace();
            limsJson.setMsg("请求错误");
        }
       return limsJson;
    }


    /**
     * 创建作者:   陈东虎
     * 创建日期:   14:39 2019/11/6
     * 方法介绍:   单独查询经验库某个设备经验详情
     * 参数说明:   EXPER_ID
     * @return    LimsJson<LimsEquipExperience>
     */
    @Override
    public LimsJson<LimsEquipExperience> selectEquipmentByExperId(HttpServletRequest request,int experId,String dataType){
        LimsJson limsJson = new LimsJson(1,"err");
        if(!"".equals(experId)){
            LimsEquipExperience equipExperience = experienceMapper.selectEquipmentByExperId(experId);
            if (equipExperience != null) {
                if("str".equals(dataType)){
                    limsJson.setMsg("查询成功");
                    limsJson.setCode(0);
                    limsJson.setObject(equipExperience);
                }else {
                    if (equipExperience.getFaultType() != null || !"".equals(equipExperience.getFaultType())) {
                        List<SysCode> sysCodes = sysCodeMapper.selectAllCommonCode(equipExperience.getFaultType());  //现象分类
                        if (sysCodes.size() > 0) {
                            equipExperience.setFaultType(sysCodes.get(0).getCodeName());
                        }
                    }
                    if (equipExperience.getInRepairMode() != null || !"".equals(equipExperience.getInRepairMode())) {
                        List<SysCode> sysCodes = sysCodeMapper.selectAllCommonCode(equipExperience.getInRepairMode()); //内部维修方式
                        if (sysCodes.size() > 0) {
                            equipExperience.setInRepairMode(sysCodes.get(0).getCodeName());
                        }
                    }
                    if (equipExperience.getOutRepairMode() != null || !"".equals(equipExperience.getOutRepairMode())) {
                        List<SysCode> sysCodes = sysCodeMapper.selectAllCommonCode(equipExperience.getOutRepairMode()); //内部维修方式
                        if (sysCodes.size() > 0) {
                            equipExperience.setOutRepairMode(sysCodes.get(0).getCodeName());
                        }
                    }
                    if (equipExperience.getInRepairRank() != null || !"".equals(equipExperience.getInRepairRank())) {
                        List<SysCode> sysCodes = sysCodeMapper.selectAllCommonCode(equipExperience.getInRepairRank());//内部维修方式
                        if (sysCodes.size() > 0) {
                            equipExperience.setInRepairRank(sysCodes.get(0).getCodeName());
                        }
                    }
                    if (equipExperience.getOutRepairRank() != null || !"".equals(equipExperience.getOutRepairRank())) {
                        List<SysCode> sysCodes = sysCodeMapper.selectAllCommonCode(equipExperience.getOutRepairRank());//内部维修方式
                        if (sysCodes.size() > 0) {
                            equipExperience.setOutRepairRank(sysCodes.get(0).getCodeName());
                        }
                    }
                    limsJson.setMsg("查询成功");
                    limsJson.setCode(0);
                    limsJson.setObject(equipExperience);
                }
            }else {
                    limsJson.setMsg("未找到数据");
                }
        }else{
            limsJson.setMsg("参数无效");
        }
        return limsJson;
    }



    /**
     * 创建作者:   陈东虎
     * 创建日期:   14:39 2019/11/6
     * 方法介绍:   根据下拉选择字段模糊查询经验库
     * 参数说明:   fieldName 字段名 fieldValue模糊查询的值
     * @return    LimsJson<LimsEquipExperience>
     */
    @Override
    public LimsJson<LimsEquipExperience> selectExperienceByName(HttpServletRequest request,String fieldName,String fieldValue,Integer page,Integer limit){
        LimsJson limsJson = new LimsJson(1,"err");
        List<LimsEquipExperience> experiencesList = new ArrayList<>();
        Map map = new HashMap();
        int count = 0;
        Map<String,String> countMap = new HashMap<>();
        //封装分页参数
        if(fieldName!=null&&!"".equals(fieldName)){
            if(!fieldName.equals("EXPER_NO")&&
                    !fieldName.equals("EQUIP_NAME")&&
                    !fieldName.equals("EQUIP_NO")&&
                    !fieldName.equals("EQUIP_TYPE_NAME")&&
                    !fieldName.equals("FACTORY_NO")&&
                    !fieldName.equals("SPEC")&&
                    !fieldName.equals("MODEL_NO")&&
                    !fieldName.equals("FAULT_TYPE")&&
                    !fieldName.equals("FAULT_POSITION")&&
                    !fieldName.equals("FAULT_DESC")){
                limsJson.setMsg("参数错误");
                return limsJson;
            }
            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(true);
            pageParams.setPage(Integer.valueOf(page));
            pageParams.setPageSize(Integer.valueOf(limit));
            map.put("pageParams", pageParams);
                countMap.put("fieldName",fieldName);
                countMap.put("fieldValue",fieldValue);

                map.put("fieldName",fieldName);
                if("EQUIP_NO".equals(fieldName)){
                    countMap.put("equipNo",fieldName);
                    map.put("equipNo",fieldName);
                }else{
                    map.put("equipNo",null);
                    countMap.put("equipNo",null);
                }
                map.put("fieldValue",fieldValue);
                experiencesList = experienceMapper.selectEquipExperienceByName(map);
                count = pageParams.getTotal();
            if(experiencesList.size()>0&&count>0){
                limsJson.setMsg("查询成功，查询到"+count+"条数据");
                limsJson.setCount(count);
                limsJson.setCode(0);
                limsJson.setData(experiencesList);
            }else{
                limsJson.setMsg("未找到数据");
            }
        }else{
            limsJson.setMsg("参数无效");
        }
        return limsJson;
    }

    /**
     * 创建作者:   陈东虎
     * 创建日期:   14:39 2019/11/6
     * 方法介绍:   根据搜索条件筛选查询 ----暂时没用到
     * 参数说明:   EquipExperience
     * @return    LimsJson<LimsEquipExperience>
     */
    @Override
    public LimsJson<LimsEquipExperience> selectExperienceByCondition(HttpServletRequest request,LimsEquipExperience experience,Integer page,Integer limit){
        LimsJson limsJson = new LimsJson(1,"err");
        if(experience!=null){
            Map map = new HashMap();
            //封装分页参数
            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(true);
            pageParams.setPage(Integer.valueOf(page));
            pageParams.setPageSize(Integer.valueOf(limit));
            map.put("pageParams", pageParams);
            map.put("experience",experience);
            List<LimsEquipExperience> experienceList = experienceMapper.selectExperienceByCondition(map);
            if(experienceList.size()>0){
                limsJson.setMsg("查询成功");
                limsJson.setCount(pageParams.getTotal());
                limsJson.setData(experienceList);
                limsJson.setCode(0);
            }else{
                limsJson.setMsg("没有数据");
            }
        }else{
            limsJson.setMsg("参数无效");
        }
        return limsJson;
    }

    /**
     * 创建作者:   陈东虎
     * 创建日期:   14:39 2019/11/6
     * 方法介绍:   添加经验库
     * 参数说明:   EquipExperience
     * @return    LimsJson
     */
    @Override
    public LimsJson addExperience(HttpServletRequest request,LimsEquipExperience equipExperience){
        LimsJson limsJson = new LimsJson(1,"err");
        try {
            if(equipExperience!=null&&equipExperience.getEquipId()!=null){
                String lastExperNo = experienceMapper.getLastExperNo();
                //JY20200327002
                String nowDataStr = DateFormat.getStrDate(new Date());
                String[] dateStr = nowDataStr.split(" ");
                String str = dateStr[0].replace("-","");
                String lastStr ="";
                String experNo="";
                if(lastExperNo==null){
                    lastExperNo ="JY"+str+"001";
                }
                String eqExperNo = lastExperNo.substring(2,10);
                int lastInt =  Integer.parseInt(lastExperNo.substring(10,13));
                switch (String.valueOf(lastInt).length()){
                    case 1:
                        lastStr = "00"+(lastInt+1);
                        break;
                    case 2:
                        lastStr = "0"+(lastInt+1);
                        break;
                    case 3:
                        lastStr =String.valueOf(lastInt+1);
                        break;
                    default:
                        lastStr = "001";
                        break;
                }
                if(str.equals(eqExperNo)){
                   experNo ="JY"+str+lastStr;
                }else{
                    experNo="JY"+str+"001";
                }
                equipExperience.setExperNo(experNo);
                int result = experienceMapper.addExperience(equipExperience);

                if(result>0){
                    limsJson.setMsg("添加成功");
                    limsJson.setCode(0);
                }else{
                    limsJson.setMsg("添加失败");
                }
            }else{
                limsJson.setMsg("参数无效");
            }
        }catch (Exception e){
            limsJson.setMsg("添加失败");
        }
        return limsJson;
    }


    /**
     * 创建作者:   陈东虎
     * 创建日期:   14:39 2019/11/6
     * 方法介绍:   删除经验库下某个设备经验
     * 参数说明:   ExperNo
     * @return    LimsJson
     */
    @Override
    public LimsJson delExperienceByExperId(HttpServletRequest request,String experIds){
        LimsJson limsJson = new LimsJson(1,"err");
        try{
            if(experIds!=null&&!"".equals(experIds)){
                String[] experIdArr = experIds.split(",");
                if(experIdArr.length>0){
                    int result = 0;
                    for(String experId : experIdArr){
                        int exper= Integer.parseInt(experId);
                        if(experId!=null&&!"".equals(experId)){
                                LimsEquipExperience equipExperience = experienceMapper.selectByPrimaryKey(exper);
                                    if(equipExperience!=null){
                                        result+=experienceMapper.delExperienceByExperId(exper);
                                    }else{
                                        continue;
                                    }
                        }else{
                            continue;
                        }
                    }
                    if(result>0){
                        limsJson.setMsg("删除成功,删除"+result+"个");
                        limsJson.setCode(0);
                    }else{
                        limsJson.setMsg("删除失败");
                    }
                }else{
                    limsJson.setMsg("参数无效");
                }
            }else{
                limsJson.setMsg("参数无效");
            }
        }catch (Exception e){
            e.printStackTrace();
            limsJson.setMsg("删除失败");
        }
        return limsJson;
    }

    /**
     * 创建作者:   陈东虎
     * 创建日期:   14:39 2019/11/12
     * 方法介绍:   修改经验库
     * 参数说明:   ExperNo
     * @return    LimsJson
     */
    @Override
    public LimsJson editExperience(HttpServletRequest request,LimsEquipExperience limsEquipExperience){
        LimsJson limsJson = new LimsJson(1,"err");

        int count =experienceMapper.editExperience(limsEquipExperience);
        if(limsEquipExperience!=null){
            if(count>0){
                limsJson.setCount(count);
                limsJson.setCode(0);
                limsJson.setMsg("修改成功");
            }else{
                limsJson.setMsg("修改失败");
            }
        }else{
            limsJson.setMsg("参数无效");
        }
        return limsJson;
    }
    /**
     * 创建作者:   陈东虎
     * 创建日期:   14:39 2019/11/6
     * 方法介绍:   查询维修方式 维修等级  查询设备故障分类
     * 参数说明:   ExperNo
     * @return    LimsJson
     */
    @Override
    public LimsJson getExperienceByEquipType(HttpServletRequest request,Integer equipType,Integer limit,Integer page){
        LimsJson limsJson = new LimsJson(1,"err");
        Map map = new HashMap();
        //封装分页参数
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(true);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("pageParams", pageParams);
        map.put("equipType",equipType);
         List<LimsEquipExperience> experienceList= experienceMapper.getExperienceByEquipType(map);
         if(experienceList.size()>0){
             for(LimsEquipExperience experience : experienceList){
                 if(experience.getFaultType()!=null){
                     SysCode sysCode = sysCodeMapper.selectCodeTypeName(experience.getFaultType());
                     if(sysCode!=null){
                         experience.setFaultType(sysCode.getCodeName());
                     }
                 }
             }
            limsJson.setCode(0);
            limsJson.setData(experienceList);
            limsJson.setMsg("查询成功");
            limsJson.setCount(pageParams.getTotal());
        }else{
            limsJson.setMsg("暂无数据");
        }

        return limsJson;
    }

}
